package com.team7.system.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team7.common.entity.Role;
import com.team7.common.entity.UserRole;
import com.team7.common.vo.CommonResult;
import com.team7.common.vo.RoleVo;
import com.team7.common.vo.UserRoleVo;
import com.team7.system.dao.RoleDao;
import com.team7.system.dao.RolePermissionDao;
import com.team7.system.dao.UserRoleDao;
import com.team7.system.service.RoleService;
import org.bouncycastle.cms.PasswordRecipientId;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: team7-Object
 * @description:
 * @author: 杨海涛
 * @create: 2021-10-19 20:13
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;
    @Resource
    private RolePermissionDao rolePermissionDao;
    @Resource
    private UserRoleDao userRoleDao;

    @Override
    public CommonResult findRoleByUserId(String userId) {
        HashMap<String, Object> map = new HashMap<>();
        System.out.println("1111");
        //无条件，查询所有角色
        List<Role> roles = roleDao.selectList(null);
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        System.out.println("22222");
        List<UserRole> userRoles = userRoleDao.selectList(wrapper);
        List<String> userRoleIds = userRoles.stream().map(item -> item.getRoleId()).collect(Collectors.toList());
        map.put("allRole",roles);
        map.put("userRoleIds",userRoleIds);
        System.out.println("33333");
        return new CommonResult(2000,"查询成功",map);
        //return null;
    }

    @Override
    @Transactional//声明式事务管理
    public CommonResult fenUserRole(UserRoleVo userRoleVo) {
        QueryWrapper<UserRole> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",userRoleVo.getUserId());
        userRoleDao.delete(wrapper);
        for(String roleId:userRoleVo.getRoleIds()) {
            UserRole userRole=new UserRole();
            userRole.setGmtCreate(LocalDateTime.now().toString());
            userRole.setGmtModified(LocalDateTime.now().toString());
            userRole.setIsDeleted(0);
            userRole.setRoleId(roleId);
            userRole.setUserId(userRoleVo.getUserId());
            userRoleDao.insert(userRole);
        }
        return new CommonResult(2000,"分配角色成功",null);
        //return null;
    }

    //分页查询
    @Override
    public CommonResult findRoleByPage(Integer page, Integer limit, RoleVo roleVo) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        //名字查询
        if (StringUtils.isNotEmpty(roleVo.getRoleName())){
            wrapper.like("role_name",roleVo.getRoleName());
        }
        if (roleVo.getIsDeleted()!=null){
            wrapper.eq("is_deleted",roleVo.getIsDeleted());
        }
        Page<Role> rolePage = new Page<>(page,limit);
        Page<Role> rolePage1 = roleDao.selectPage(rolePage, wrapper);
        return new CommonResult(2000,"查询成功",rolePage1);
        //return null;
    }

    //根据角色名字查询
    @Override
    @Transactional
    public CommonResult findRoleByRoleName(Integer page, Integer limit, RoleVo roleVo) {
        Page<Role> rolePage = new Page<>(page,limit);
        if (StringUtils.isNotEmpty(roleVo.getRoleName())){
            QueryWrapper<Role> wrapper = new QueryWrapper<>();
            wrapper.like("role_name",roleVo.getRoleName());
            Page<Role> rolePage1 = roleDao.selectPage(rolePage, wrapper);
            return new CommonResult(2000,"查询成功",rolePage1);

        }else{
            Page<Role> rolePage1 = roleDao.selectPage(rolePage, null);
            return new CommonResult(2000,"查询成功",rolePage1);
        }
        //return null;
    }

    //根据Id修改角色状态
    @Override
    @Transactional//声明式事务管理
    public CommonResult updateRoleStatusByid(String id, Integer deleted) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        List<Role> roles = roleDao.selectList(wrapper);
        //保护超级管理员不被修改
        for (Role role:roles) {
            if ("1".equals(role.getId())){
                return new CommonResult(5000,"超级管理员无法禁用",null);
            }
        }
        //1.改变角色表中的角色状态
        Role role = new Role();
        role.setId(id);
        role.setIsDeleted(deleted);
        role.setGmtModified(LocalDateTime.now().toString());
        int i = roleDao.updateById(role);
        //2.改变用户角色表中的角色状态
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",id);
        UserRole userRole = new UserRole();
        userRole.setIsDeleted(deleted);
        userRole.setGmtModified(LocalDateTime.now().toString());
        int i1 = userRoleDao.update(userRole, queryWrapper);
        return new CommonResult(2000,"状态修改成功",null);
        //return null;
    }

    //根据roleId进行删除
    @Override
    @Transactional//声明式事务管理
    public CommonResult deleteRoleById(String roleid) {
        int i1 = roleDao.deleteById(roleid);//删除角色表中的角色
        int i2 = rolePermissionDao.deleteByRoleId(roleid);//删除角色权限表中的角色
        int i3 = userRoleDao.deleteByRoleId(roleid);//删除用户角色表中的角色
        //是否需要对i1,i2,i3进行是否删除成功的判断？
        return new CommonResult(2000,"角色删除成功",null);
        //return null;
    }

    //新增和修改
    @Override
    public CommonResult addOrUpdateRole(Role role) {
        //1.根据id是否为空来判断新增还是修改
        if (StringUtils.isNotEmpty(role.getId())){
            //1.1修 改
            int i = roleDao.updateById(role);
            return new CommonResult(2000,"角色修改成功",null);
        }
        //1.2 新增
        role.setRoleCode("1");
        int insert = roleDao.insert(role);
        return new CommonResult(2000,"角色新增成功",null);
        //return null;
    }
}
